package com.dongdongshop.sms;

import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Service
@RocketMQMessageListener(topic = "MyTopic",consumerGroup = "myGroup")
public class SendSms implements RocketMQListener<String> {

    @Resource
    private com.dongdongshop.util.SendSmsUtil sendSmsUtil;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void onMessage(String phoneNumber) {
        try {
            //返回生成消息的验证码
            String code = sendSmsUtil.sendSms(phoneNumber);
            //将验证码放缓存
            redisTemplate.boundValueOps(phoneNumber).set(code);
            //给当前验证码设置一个有效时间
            redisTemplate.boundValueOps(phoneNumber).expire(15, TimeUnit.MINUTES);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
